package com.jiajunsong.blog.domain;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;

@Entity
public class Blog {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Size(min = 2, max = 50)
	@Column(nullable = false, length = 50)
	private String title;

	@Size(min = 2, max = 300)
	@Column(nullable = false)
	private String summary;

	@Lob
	@Size(min = 2)
	@Column(nullable = false)
	private String htmlContent;

	@Column(nullable = false)
	@CreationTimestamp
	private LocalDateTime createTime;

	private String avatar;
	private String tags;

	private Integer readSize = 0;
	private Integer commentSize = 0;
	private Integer voteSize = 0;

	@ManyToOne(fetch = FetchType.EAGER)
	private User user;

	@ManyToOne(fetch = FetchType.EAGER)
	private Catalog catalog;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
	@JoinTable(name = "blog_comments", joinColumns = @JoinColumn(name = "blog_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "comment_id", referencedColumnName = "id"))
	// orphanRemoval=false是默认值，把comment从comments里删除后在保存blog，会删除中间表里的对应值(中间表的名称是blog_comments),但是并不会把对应的comment删除，删除中间表内的数据是先删除再插入
	// orphanRemoval=false设置为false时，从comments里移除的comment不会被删除，只会删除中间表里对应的关系;注意这里cascade=CascadeType.ALL
	// 如果把此blog删除，则对应的comment无论orphanRemoval如何设置都会被删除 
	// 如果cascade没有设置，则任何操作都不会级联，所做的操作只能影响blog本身和中间表，则无论fetch如何都不会影响到comment表
	//必须初始化，不然的话下面的addComment方法就会有问题，因为comments的值还是null,comments.add()肯定会出错，
	//如果此处不初始化，但是先用blogRepository.findOne()初始化了一个blog,这时comments就被自动初始化了
	private List<Comment> comments= new ArrayList<>();
	
	public Blog() {

	}

	public Blog(String title, String summary) {
		this.title = title;
		this.summary = summary;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getHtmlContent() {
		return htmlContent;
	}

	public void setHtmlContent(String htmlContent) {
		this.htmlContent = htmlContent;
	}

	public LocalDateTime getCreateTime() {
		return createTime;
	}

	public void setCreateTime(LocalDateTime createTime) {
		this.createTime = createTime;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getReadSize() {
		return readSize;
	}

	public void setReadSize(Integer readSize) {
		this.readSize = readSize;
	}

	public Integer getCommentSize() {
		return commentSize;
	}

	public void setCommentSize(Integer commentSize) {
		this.commentSize = commentSize;
	}

	public Integer getVoteSize() {
		return voteSize;
	}

	public void setVoteSize(Integer voteSize) {
		this.voteSize = voteSize;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public Catalog getCatalog() {
		return catalog;
	}

	public void setCatalog(Catalog catalog) {
		this.catalog = catalog;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public void addComment(Comment comment) {
		comments.add(comment);
		commentSize = comments.size();
	}

	// 如果comments里面包含commentId,返回true，否则false，
	// 用来确定要删除的commentId是否属于此blog,如果属于才允许删除评论，
	public boolean removeComment(Long commentId) {
		boolean isIn = false;
		for (int i = 0; i < comments.size(); i++) {
			if (comments.get(i).getId().equals(commentId)) {
				comments.remove(i);
				isIn = true;
				break;
			}
		}
		commentSize = comments.size();
		return isIn;
		
	}
	
	public boolean removeComment(Comment comment) {
		boolean isIn = false;
		if(comments.contains(comment)) {
			isIn=true;
			comments.remove(comment);
		}
		return isIn;
	}

}
